//
//  LDSPersonalViewControllerViewModel.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/10/19.
//Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSPersonalViewControllerViewModel.h"
#import "UIResponder+Router.h"

@interface LDSPersonalViewControllerViewModel ()

@end

@implementation LDSPersonalViewControllerViewModel

- (void)loadFetchData {
    NSMutableArray  *dataSource = [NSMutableArray array];
    NSObject *model1 = [LDSPersonalModel initWithImageName:nil andText:@"Name" ];
    NSObject *model2 = [LDSPersonalModel initWithImageName:nil andText:@"Age" ];
    NSObject *model3 = [LDSPersonalModel initWithImageName:nil andText:@"Gender" ];
    NSArray *modelArray = @[model1,model2,model3];
    
    NSMutableArray *sectionArray = [NSMutableArray array];
    for (NSInteger i = 0; i< modelArray.count; i++) {
        LDSTableViewCellViewModel *cellviewModel = [LDSTableViewCellViewModel modelCellWithClass:[LDSPersonalTableCell class] model:modelArray[i]  tag:i];
        cellviewModel.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cellviewModel.tableViewCellStyle = UITableViewCellStyleSubtitle;
        [sectionArray addObject:cellviewModel];
    }
    [dataSource addObject:sectionArray];
    self.fetchDataSource = dataSource;
    [self setUpDataSource];
}

- (void)setUpDataSource {
    if (self.fetchDataSource) {
        self.dataSource = self.fetchDataSource;
        if ([self.delegate respondsToSelector:@selector(lds_didReceiveDataWithDataSource:andSectionDataSource:)]) {
            [self.delegate lds_didReceiveDataWithDataSource:[self.dataSource copy]
                                       andSectionDataSource:[self.sectionDataSource copy]];
        }
    }
    
}

- (void)handleWithPickView:(UIPickerView *)pickView {
    pickView.delegate = (id<UIPickerViewDelegate>)self;
    pickView.dataSource = (id<UIPickerViewDataSource> )self;
}

- (void)handleWithImagePickerController:(UIImagePickerController *)imagePickerController {
    imagePickerController.delegate = (id <UINavigationControllerDelegate, UIImagePickerControllerDelegate>)self;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 100;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat:@"%ld",(long)row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED{
    return 44;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED{
    NSDictionary *dic = @{@"detail":[NSString stringWithFormat:@"%ld",row],
                          @"row":@(pickerView.tag)};
    [pickerView routerEventWithName:@"pickView" userInfo:dic];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]) {
        UIImage * pickerImage=[info objectForKey:UIImagePickerControllerEditedImage];
        NSDictionary *dic = @{@"image":pickerImage };
        [picker routerEventWithName:@"avatar" userInfo:dic];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


@end
